package com.jarasoft.AMP3MP;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.provider.MediaStore;
import android.widget.RemoteViews;

public class AMP3MPWidget extends AppWidgetProvider {

	private String curlist;
	private SharedPreferences pref;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		// Iteramos la lista de widgets en ejecución

		for (int i = 0; i < appWidgetIds.length; i++) {
			// Actualizamos el widget actual
			actualizarWidget(context, appWidgetManager, appWidgetIds[i], 1, 0, null);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		boolean flag = false;
		if (intent.getAction().equals(AndroidMP3MusicPlayerService.PLAYPAUSEACTION)) {
			if (!AndroidMP3MusicPlayerService.isRunning() && !AndroidMP3MusicPlayerActivity.isRunning()) {
				Intent i = new Intent(context, AndroidMP3MusicPlayerService.class);
				pref = context.getSharedPreferences("controlmp3", Context.MODE_WORLD_WRITEABLE);
				curlist = pref.getString("LIST", MediaStore.Audio.Media.ARTIST);
				i.putExtra("CURLIST", curlist);
				context.startService(i);
			}
			flag = true;
		}
		if (intent.getAction().equals(AndroidMP3MusicPlayerService.PLAYBACTION)
		      || intent.getAction().equals(AndroidMP3MusicPlayerService.PAUSEBACTION) || flag) {
			ComponentName wgt = new ComponentName(context, AMP3MPWidget.class);
			AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
			int[] appWidgetIds = widgetManager.getAppWidgetIds(wgt);
			for (int i = 0; i < appWidgetIds.length; i++)
				actualizarWidget(context, widgetManager, appWidgetIds[i], -1, 0, null);
		}
		if (intent.getAction().equals(AndroidMP3MusicPlayerService.PROGRESSACTION)) {
			ComponentName wgt = new ComponentName(context, AMP3MPWidget.class);
			AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
			int[] appWidgetIds = widgetManager.getAppWidgetIds(wgt);
			int max = intent.getExtras().getInt("MAXPB");
			int curr = intent.getExtras().getInt("PROPB");
			for (int i = 0; i < appWidgetIds.length; i++)
				actualizarWidget(context, widgetManager, appWidgetIds[i], max, curr, null);
		}
		if (intent.getAction().equals(AndroidMP3MusicPlayerService.SONGINFOACTION)) {
			ComponentName wgt = new ComponentName(context, AMP3MPWidget.class);
			AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
			int[] appWidgetIds = widgetManager.getAppWidgetIds(wgt);
			String songinfo = intent.getExtras().getString("SONGINFO");
			for (int i = 0; i < appWidgetIds.length; i++)
				actualizarWidget(context, widgetManager, appWidgetIds[i], -1, 0, songinfo);
		}
		super.onReceive(context, intent);

	}

	public static void actualizarWidget(Context context, AppWidgetManager appWidgetManager, int widgetId, int max,
	      int curr, String songinfo) {

		// Obtenemos la lista de controles del widget actual
		RemoteViews controles = new RemoteViews(context.getPackageName(), R.layout.widgetplayer);

		Intent intent = new Intent(AndroidMP3MusicPlayerService.PREVACTION);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, widgetId, intent,
		      PendingIntent.FLAG_UPDATE_CURRENT);
		controles.setOnClickPendingIntent(R.id.bprevw, pendingIntent);

		intent = new Intent(AndroidMP3MusicPlayerService.PLAYPAUSEACTION);
		pendingIntent = PendingIntent.getBroadcast(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		controles.setOnClickPendingIntent(R.id.bplayw, pendingIntent);

		intent = new Intent(AndroidMP3MusicPlayerService.STOPACTION);
		pendingIntent = PendingIntent.getBroadcast(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		controles.setOnClickPendingIntent(R.id.bstopw, pendingIntent);

		intent = new Intent(AndroidMP3MusicPlayerService.NEXTACTION);
		pendingIntent = PendingIntent.getBroadcast(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		controles.setOnClickPendingIntent(R.id.bnextw, pendingIntent);

		if (AndroidMP3MusicPlayerService.isPause() || !AndroidMP3MusicPlayerService.isRunning()) {
			controles.setInt(R.id.bplayw, "setBackgroundResource", R.drawable.b_play);
		} else {
			controles.setInt(R.id.bplayw, "setBackgroundResource", R.drawable.b_pause);
		}

		if (max >= 0) {
			controles.setInt(R.id.progressBarw, "setMax", max);
			controles.setInt(R.id.progressBarw, "setProgress", curr);
		}
		if (songinfo != null) {
			controles.setTextViewText(R.id.songinfow, songinfo);
		}

		// Notificamos al manager de la actualización del widget actual
		appWidgetManager.updateAppWidget(widgetId, controles);
	}

}
